home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / ICON / ICONV8 / Source / Common / C / Mktemp < prev    next >
Text File  |  1990-09-18  |  4KB  |  194 lines

  1. /* C.Mktemp: Make a temporary file name */
  2.  
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "kernel.h"
  6. #include "swis.h"
  7.  
  8. #define ReadCat    5
  9.  
  10. /* Create a temporary file name by adding a directory prefix to file.
  11.  * If the external variable temp_dir is not zero, this directory will be
  12.  * used. Otherwise, the following are used, in order.
  13.  *   1. <Tmp$Dir>
  14.  *   2. &.Tmp
  15.  *   3. The current directory.
  16.  * The function returns zero on an error (temp_dir is not a directory, or
  17.  * malloc() failed), otherwise it returns a malloc-ed string containing
  18.  * the required name.
  19.  */
  20.  
  21. static char *concat (const char *dir, const char *file);
  22.  
  23. char *temp_dir = 0;
  24.  
  25. char *mktemp (const char *file)
  26. {
  27.     char *dir;
  28.     char *name;
  29.     char buf[11];
  30.     int len = strlen(file);
  31.     _kernel_osfile_block blk;
  32.     _kernel_swi_regs regs;
  33.  
  34.     /* Is the supplied filename a pure file name? */
  35.     if (len > 10)
  36.         return 0;
  37.  
  38.     /* Pad out the supplied filename on the left with a unique ID
  39.      * (Based on the program start time)
  40.      */
  41.     if (len < 10 && _kernel_swi(OS_GetEnv,®s,®s) == NULL)
  42.     {
  43.         int i;
  44.         char *time = (char *)regs.r[2];
  45.  
  46.         strcpy(buf,file);
  47.  
  48.         for (i = len; i < 10; ++i)
  49.         {
  50.             char c = time[(9 - i) >> 1];
  51.  
  52.             if (i & 1)
  53.                 c >>= 4;
  54.  
  55.             c &= 0x0F;
  56.             buf[i] = "abcdefghijklmnop"[c];
  57.         }
  58.  
  59.         buf[10] = 0;
  60.  
  61.         file = buf;
  62.     }
  63.  
  64.     /* First, try the supplied directory */
  65.     if ( temp_dir )
  66.     {
  67.         if ( _kernel_osfile(ReadCat,temp_dir,&blk) == 2 )
  68.             return concat(temp_dir,file);
  69.         else
  70.         {
  71.             /* Is it a filing system name only? */
  72.             len = strlen(temp_dir);
  73.  
  74.             if (temp_dir[len-1] != ':')
  75.                 return 0;
  76.  
  77.             /* One extra, just in case file == "", for the '@' */
  78.             name = malloc(len + strlen(file) + 2);
  79.  
  80.             if (name == 0)
  81.                 return 0;
  82.  
  83.             strcpy(name,temp_dir);
  84.             name[len] = '@';
  85.             name[len+1] = '\0';
  86.  
  87.             if (_kernel_osfile(ReadCat,name,&blk) != 2)
  88.             {
  89.                 free(name);
  90.                 return 0;
  91.             }
  92.  
  93.             strcpy(&name[len],file);
  94.             return name;
  95.         }
  96.     }
  97.  
  98.     /* Otherwise, go through the list... */
  99.  
  100.     /* First of all, try <Tmp$Dir> */
  101.     if ((dir = getenv("Tmp$Dir")) != 0)
  102.     {
  103.         if (_kernel_osfile(ReadCat,dir,&blk) == 2)
  104.             return concat(dir,file);
  105.         else
  106.         {
  107.             /* Is it a filing system name only? */
  108.             len = strlen(dir);
  109.  
  110.             if (dir[len-1] != ':')
  111.                 goto no_go;
  112.  
  113.             /* One extra, just in case file == "", for the '@' */
  114.             name = malloc(len + strlen(file) + 2);
  115.  
  116.             if (name == 0)
  117.                 goto no_go;
  118.  
  119.             strcpy(name,dir);
  120.             name[len] = '@';
  121.             name[len+1] = '\0';
  122.  
  123.             if (_kernel_osfile(ReadCat,name,&blk) != 2)
  124.             {
  125.                 free(name);
  126.                 goto no_go;
  127.             }
  128.  
  129.             strcpy(&name[len],file);
  130.             return name;
  131.         }
  132.     }
  133.  
  134. no_go:
  135.     /* No <Tmp$Dir>, so try &.Tmp, if it exists */
  136.     if (_kernel_osfile(ReadCat,"&.Tmp",&blk) == 2)
  137.         return concat("&.Tmp",file);
  138.  
  139.     /* Out of luck - use the current directory */
  140.     name = malloc(strlen(file)+1);
  141.     if ( name )
  142.         strcpy(name,file);
  143.  
  144.     return name;
  145. }
  146.  
  147. static char *concat (const char *dir, const char *file)
  148. {
  149.     char *result = malloc(strlen(dir)+strlen(file)+2);
  150.     char *p = result;
  151.  
  152.     if ( result == 0 )
  153.         return 0;
  154.  
  155.     while ( *dir )
  156.         *p++ = *dir++;
  157.  
  158.     *p++ = '.';
  159.     while ( *file )
  160.         *p++ = *file++;
  161.  
  162.     *p = '\0';
  163.  
  164.     return result;
  165. }
  166.  
  167. /* ----------------------------------------------------------------- */
  168.  
  169. #ifdef test
  170.  
  171. #include <stdio.h>
  172.  
  173. int main (int argc, char *argv[])
  174. {
  175.     char *tmp;
  176.  
  177.     if ( argc != 2 && argc != 3 )
  178.     {
  179.         fprintf(stderr,"Usage: %s file [dir]\n",argv[0]);
  180.         return 1;
  181.     }
  182.  
  183.     if ( argc == 3 )
  184.         temp_dir = argv[2];
  185.  
  186.     tmp = mktemp (argv[1]);
  187.  
  188.     printf("Temp file = %s\n", tmp ? tmp : "<Not possible>");
  189.  
  190.     return 0;
  191. }
  192.  
  193. #endif
  194.